const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}

function requestLoading(url, data, method) {
  return new Promise((resolve) => {
    wx.request({
      url: url,
      data: data,
      method: method,
      success: function(res) {
        if (res.statusCode == 200) {
          resolve(res)
        } else {
          fail()
        }
      },
      error: function(error) {
        resolve(error)
        wx.showModal({
          content: `请求失败，错误码：${error.status}`,
          showCancel: false,
          success: function(res) {
            if (res.confirm) {
              console.log('用户点击确定')
            }
          }
        });
      }
    })
  })
}

module.exports = {
  formatTime: formatTime,
  requestLoading: requestLoading
}